<?php
/**
 * 购物车管理
 * @author hushuai
 *
 */
class CartAction extends CommonAction {

	/**
	 * 定义数据表和查询视图
	 */
	private $Cart            = "Cart";             //定义购物车表
	private $Dishes          = "Dishes";           //定义菜品表
	private $Drink           = "Drink";            //定义饮品表
	private $DishesSortView  = "DishesSortView";   //定义菜品fenlei关联视图

	/**
	 * 定义返回消息
	 */
	
	private $cart_add_error  = "添加到购物车失败";
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 将商品插入购物车
     */
    
    public function addtoCart(){
    	
    	$dataid   = $this->_req("dataid");
    	$type     = $this->_req("type");
		$admin    = $this->_req("admin","intval",2);
    	
    	$id       = $this->_req("id");
    	$solevar  = $this->_req("solevar");
    	$flags    = $this->_req("flag");
    	$gnfr     = $this->_req("gnfr")?json_encode($this->_req("gnfr")):"";
    	$part     = $this->_req("part");
    	$isspec   = $this->_req("isspec");
    	$price    = $this->_req("price");
    	$psPrice  = $this->_req("psPrice");
    	$dprice   = $this->_req("dprice");
    	$dishflag = $this->_req("dishflag");
    	$outletid = $this->_req("outletid");
		
    	//判断商品的唯一标识符是否存在，若果不存在则一商品id作为唯一标示符
    	if(!$dishflag) $dishflag = $id;
    	
    	if($flags=="drink"){
    		$model = M($this->Drink);
    	}else{
    		$model  = M($this->Dishes);
    	}
    	
    	//根据商品标识和添加人编号来查询该商品在购物车中是否存在
    	$con = array("dishflag"=>$dishflag,"dataid"=>$dataid,"outletid"=>$outletid);
    	$flag = M($this->Cart)->where($con)->find();
    	
    	if($flag && !$admin){
    		//如果购物车中存在该商品则该更新该商品的数量和总价
    		$dnum['num']     = intval($flag['num'])+1;
    		$dnum['xjprice'] = intval($flag['price'])*$dnum['num'];
    		$data = M($this->Cart)->where($con)->save($dnum);
    		if($data){
    			//如果操作成功则返回购物车列表
    			$result = D($this->Cart)->cartlist($dataid,$outletid);
    			$this->success($result);
    		}else{
    			$this->error($this->cart_add_error);
    		}
    	}else{
    		
    		//若果购物车中不存在该商品则向购物车中插入该商品
    		$dish = $model->find($id);
    		
    		$map['solevar']     = $solevar;
    		$map['dishesid']    = $id;
    		$map['title']       = $dish['title'];
    		$map['entitle']     = $dish['entitle'];
    		$map['price']       = $price;
    		$map['psPrice']     = $psPrice;
    		$map['tprice']      = $dprice;
    		$map['mprice']      = $dish['mprice'];
    		$map['dprice']      = $dish['sprice'];
    		$map['isspec']      = $isspec;
    		$map['xjprice']     = $price ? $price : $dprice;
    		$map['dataid']      = $dataid;
    		$map['tableid']     = $type;
    		$map['gnfr']        = $gnfr;
    		$map['part']        = $part;
    		$map['flag']        = $flags;
    		$map['dishflag']    = $dishflag;
    		$map['outletid']    = $outletid;
    		$map['addtime']     = time();
			$map['admin']       = $admin;
    		$map['dishes_time'] = $dish['dishes_time'];
    		
    		if($flags=='dishes'){
    			$map['dishes_num']  = $dish['dishes_num'] ? $dish['dishes_num'] : 0;
    		}else{
    			$map['dishes_num']  = $dish['drink_num']  ? $dish['drink_num']  : 0;
    		}
    		
    		$data = D($this->Cart)->add($map);
    		if($data){
    			//如果操作成功则返回购物车列表
    			$this->success(D($this->Cart)->cartlist($dataid,$outletid));
    		}else{
    			$this->error($this->cart_add_error);
    		}
    	}
    }
	
	/**
	 * 修改购物车菜品属性
	 */
	public function edit(){
		
		$id      = $this->_req('id');
		$gnfr    = $this->_req('gnfr');
		$price   = $this->_req('price');
		$psPrice = $this->_req('psPrice');
		$model   = D($this->Cart);
		$one     = $model->find($id);		
		
		if($one['id']){
			
			$map['id']      = $id;
			$map['gnfr']    = json_encode($gnfr);
			$map['price']   = $one['dprice'] + $price;
			$map['psPrice'] = $psPrice;
			//$map['mprice']  = $one['mprice'] + $psPrice;
			//$map['dprice']  = $one['dprice'] + $price;
			$map['xjprice'] = $one['num']*$map['price'];
			$id = $model->save($map);
			
			if($id){
				$this->success($id);	
			}else{
				$this->error('商品属性添加失败，请稍后重试!');	
			}
		}else{
			$this->error('数据非法!');		
		}
	}
    
    /**
     * 修改购物车中某一菜品的数量
     * @param id  int
     * @param num int
     */
    public function setnum(){
    	
    	$id    = $this->_req("id");
    	$num   = $this->_req("num");
    	$price = $this->_req("price");
		
		if(empty($price)){
			$model = M('Cart');
			$one   = $model->find($id);
			$price = $one['price']*$num;
		}
    	
    	$con  = array("id"=>$id,"num"=>$num,"xjprice"=>$price);
    	$this->success(M($this->Cart)->save($con));
    
    }
    
    /**
     * 修改购物车中某一菜品的备注
     * @param id  int
     * @param num int
     */
    public function editask(){
    	 
    	$map['id']    = $this->_req("id");
    	$map['ask']   = $this->_req("ask");
    	$this->success(M($this->Cart)->save($map));
    
    }
    /**
     * 计算购物车商品总价
     * @param dataid  int
     */
    public function calculateTotal(){
    
    	$map['dataid']   = $this->_req("dataid");
    	$map['outletid'] = $this->_req("outletid"); 
    	$this->success(M($this->Cart)->where($map)->sum("xjprice"));
    }
    
    /**
     * 删除购物车中的某一菜品
     * @param id  int
     */
    public function del(){
    	
    	$id     = $this->_req("id");
    	$data   = M($this->Cart)->where("id=".$id)->delete();
    	if($data){
    		$dataid   = $this->_req("dataid");
    		$outletid = $this->_req("outletid");
    		$this->success(D($this->Cart)->cartlist($dataid,$outletid));
    	}else{
    		$this->error("Delete failed");//删除失败
    	}
    	
    }
    /**
     * 查询当前用户购物车列表
     * @param userid  int
     */
    public function view(){
    	
    	$dataid   = $this->_req("dataid");
    	$outletid = $this->_req("outletid");
		$admin    = $this->_req("admin","intval",2);
    	$this->success(D($this->Cart)->cartlist($dataid,$outletid,$admin));
    }
	
	/**
     * 查询当前用户购物车列表-分页
     * @param userid  int
     */
	public function cartlist(){
		$desc     = $this->_get("orderDirection", "trim",   "desc");;
    	$order    = $this->_get("orderName",      "trim",   "addtime");
    	$pageNum  = $this->_get("pageNum",        "intval", C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p",              "intval", 1);
    	
		$map['dataid']  = $this->_get('dataid');
		$map['tableid'] = $this->_get('tableid');
    	$map['isdel']   = 2 ;
    	$con      = 'flag desc';
    	$table    = 'Cart'; 
		
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));	
	}
    
    /**
     * 改变购物车会员信息状态
     */
    public function changeStatus(){
    	
    	$userid  = $this->_req("userid");
    	$type    = $this->_req("type");
    	
    	$nUserid = $this->_req("nUserid");
    	$nType   = $this->_req("nType");
    	
    	$map['dataid']   = $userid;
    	$map['tableid']  = $type;
    	
    	$res['userid'] = $nUserid;
    	$res['nType']  = $nType;
    	$data = M($this->Cart)->where($map)->save($res);
    	$this->success($data);
    }
	
	//查询购物车某件商品的详细信息
	public function detail(){
		$id = $this->_req('id','intval',0);
		
		if($id){
			$this->success(M($this->Cart)->find($id)); 
		}else{
			$this->error('请选择需要查询的商品');	
		}	
	}
    
}